package cn.db101.view;


import cn.db101.dao.adminDao;
import cn.db101.model.admin;
import cn.db101.util.DbUtil;
import cn.db101.util.StringUtil;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

public class alter_account_Frame{
	 JFrame jf=new JFrame() ;
	private JTextField name_textField;
	private JTextField account_textField;
	private JRadioButton man_RadioButton;
	private JRadioButton woman_RadioButton;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private DbUtil dbUtil= new DbUtil();
	

	/**
	 * Create the frame.
	 * @wbp.parser.entryPoint
	 */
	public void init() {
		jf.setTitle("\u4FEE\u6539\u8D26\u53F7");
		jf.setBounds(100, 100, 505, 653);
		
		JPanel panel = new JPanel();
		jf.getContentPane().add(panel, BorderLayout.CENTER);
		
		JPanel panel_1 = new JPanel();
		panel_1.setToolTipText("");
		panel_1.setForeground(new Color(0, 0, 0));
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u4FEE\u6539\u8D26\u6237", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(alter_account_Frame.class.getResource("/img/x.jpg")));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addComponent(lblNewLabel)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(27)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 442, GroupLayout.PREFERRED_SIZE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 258, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 342, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(58, Short.MAX_VALUE))
		);
		
		JLabel label = new JLabel("\u59D3        \u540D\uFF1A");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		JLabel label_1 = new JLabel("\u6027        \u522B\uFF1A");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		JButton button_1 = new JButton("\u4FEE\u6539");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				alter(e);
			}
		});
		button_1.setIcon(new ImageIcon(alter_account_Frame.class.getResource("/icon/edit\u7F16\u8F91.png")));
		
		name_textField = new JTextField();
		name_textField.setColumns(10);
		
		JLabel label_2 = new JLabel("\u8D26        \u53F7\uFF1A");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		account_textField = new JTextField();
		account_textField.setColumns(10);
		
	    man_RadioButton = new JRadioButton("\u7537");
		buttonGroup.add(man_RadioButton);
		man_RadioButton.setSelected(true);
		
		woman_RadioButton = new JRadioButton("\u5973");
		buttonGroup.add(woman_RadioButton);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(103)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
								.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
								.addComponent(label_1)
								.addComponent(label))
							.addGap(18)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addComponent(name_textField, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_panel_1.createSequentialGroup()
									.addGap(15)
									.addComponent(man_RadioButton)
									.addGap(18)
									.addComponent(woman_RadioButton, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE))
								.addComponent(account_textField, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(162)
							.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(96, Short.MAX_VALUE))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap(36, Short.MAX_VALUE)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(account_textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(36)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(name_textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(49)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(man_RadioButton)
						.addComponent(woman_RadioButton))
					.addGap(34)
					.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
					.addGap(46))
		);
		panel_1.setLayout(gl_panel_1);
		panel.setLayout(gl_panel);
		//���ô����Ƿ�ɸı�
		jf.setResizable(false);
		//���ô���ɼ�
		jf.setVisible(true);	
		//����JFrame������ʾ
		jf.setLocationRelativeTo(null);

	}
	/**
	 * �޸��˻��¼�����
	 * @param evt
	 */
	private void alter(ActionEvent evt) {
		String account=new String(this.account_textField.getText());
		String name=new String(this.name_textField.getText());
		if(StringUtil.isEmpty(account)) {
			JOptionPane.showMessageDialog(null,"�˺Ų���Ϊ�գ�");
			return;
		}
		if(StringUtil.isEmpty(name)) {
			JOptionPane.showMessageDialog(null,"��������Ϊ�գ�");
			return;
		}
		String gender="";
		if(man_RadioButton.isSelected())
		{
			gender="��";
		}else if(woman_RadioButton.isSelected())
		{
			gender="Ů";
		}
		admin adm=new admin(account,name,gender);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			int current= adminDao.alteraccount(con,adm);
			if(current==1) {
				JOptionPane.showMessageDialog(null, "�޸��˻��ɹ���");
			}else {
				JOptionPane.showMessageDialog(null, "�޸��˻�ʧ�ܣ�");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
